﻿using Laboratory.Users;
using Laboratory.Users.Dto;
using System;
using System.Web.Mvc;

namespace Laboratory.Web.Controllers
{
    public class UserController : Controller
    {
        private readonly IUserService _userSetvice;

        public UserController(IUserService user)
        {
            this._userSetvice = user;
        }



        // 菜单

        public ActionResult MenuList()
        {
            return View();
        }

        public ActionResult MenuEdit(Guid? id, string listPage)
        {
            ViewBag.Id = id;
            ViewBag.ListPage = listPage;
            return View();
        }

        public JsonResult GetListMenu(string keyword)
        {
            JsonResult json = new JsonResult()
            {
                Data = _userSetvice.GetMenus(keyword, 1, int.MaxValue).Items
            };

            return json;
        }

        public JsonResult GetMenu(Guid? id)
        {
            MenuDto model;
            if (id != null && id != Guid.Empty)
                model = _userSetvice.GetMenu(id.Value);
            else
                model = new MenuDto();

            JsonResult json = new JsonResult()
            {
                Data = model
            };

            return json;
        }

        public void SaveMenu(MenuDto dto)
        {
            if (dto.Id == Guid.Empty)
                _userSetvice.CreateMenu(dto.DisplayName, dto.Icon, dto.Order, dto.Url, dto.ParentId);
            else
                _userSetvice.UpdateMenu(dto.Id, dto.DisplayName, dto.Icon, dto.Order, dto.Url, dto.ParentId);
        }


        // 角色

        public ActionResult RoleList()
        {
            return View();
        }

        public ActionResult RoleEdit(Guid? id, string listPage)
        {
            ViewBag.Id = id;
            ViewBag.ListPage = listPage;
            return View();
        }

        public JsonResult GetListRole()
        {
            JsonResult json = new JsonResult()
            {
                Data = _userSetvice.GetRoles()
            };
            return json;
        }

        public JsonResult GetRole(Guid? id)
        {
            RoleDto model;
            if (id != null && id != Guid.Empty)
                model = _userSetvice.GetRole(id.Value);
            else
                model = new RoleDto();

            JsonResult json = new JsonResult()
            {
                Data = model
            };

            return json;
        }

        public void SaveRole(RoleDto dto)
        {
            if (dto.Id == Guid.Empty)
                _userSetvice.CreateRole(dto.Name, dto.Sort);
            else
                _userSetvice.UpdateRole(dto.Id, dto.Name, dto.Sort);
        }



    }
}